package com.shrcl.engineerprocess.domain.impl;

import com.google.common.base.Objects;
import com.shrcl.engineerprocess.domain.BusinessProcessDelete;
import com.shrcl.engineerprocess.domain.CostEstimationDAO;
import com.shrcl.engineerprocess.domain.CostEstimationProcesss;
import com.shrcl.engineerprocess.domain.EngineerCostEstimation;
import com.shrcl.engineerprocess.domain.EngineerProcessTaskExtension;
import com.shrcl.engineerprocess.domain.TaskExtensionDelete;
import com.shrcl.engineerprocess.domain.impl.EngineerCostEstimationImpl;
import edu.fudan.langlab.domain.workflow.BusinessProcess;
import edu.fudan.mylang.pf.BaseDAOService;
import edu.fudan.mylang.pf.IGenericQuery;
import java.util.Collection;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("com.shrcl.engineerprocess.domain.CostEstimationDAO")
public class CostEstimationDAOImpl extends BaseDAOService<EngineerCostEstimation> implements CostEstimationDAO {
  @Autowired
  private TaskExtensionDelete taskExtensionDelete;
  
  @Autowired
  private BusinessProcessDelete businessProcessDelete;
  
  public EngineerCostEstimation create(final Procedure1<? super EngineerCostEstimation> init) {EngineerCostEstimation engineerCostEstimation=EngineerCostEstimationImpl.create(getObjectFactory());
    final Procedure1<EngineerCostEstimation> _function = new Procedure1<EngineerCostEstimation>() {
        public void apply(final EngineerCostEstimation it) {
          init.apply(it);
          it.start();
        }
      };
    EngineerCostEstimation _doubleArrow = ObjectExtensions.<EngineerCostEstimation>operator_doubleArrow(engineerCostEstimation, _function);
    return _doubleArrow;
  }
  
  public EngineerCostEstimation getEngineerCostEstimationFromBp(final BusinessProcess bp) {
    IGenericQuery<EngineerCostEstimation> query=getObjectFactory().createGenericQuery(EngineerCostEstimation.class,"select e from com.shrcl.engineerprocess.domain.impl.EngineerCostEstimationImpl as e   where ((e.businessProcess) = (:bp)) and  e.active = 1      ").setParameter("bp",bp);
    EngineerCostEstimation _uniqueResult = query.uniqueResult();
    return _uniqueResult;
  }
  
  public void delete(final EngineerCostEstimation costEstimation) {getObjectFactory().delete(costEstimation);
    
    BusinessProcess _businessProcess = costEstimation.getBusinessProcess();
    boolean _notEquals = (!Objects.equal(_businessProcess, null));
    if (_notEquals) {IGenericQuery<EngineerProcessTaskExtension> query=getObjectFactory().createGenericQuery(EngineerProcessTaskExtension.class,"select e from com.shrcl.engineerprocess.domain.impl.EngineerProcessTaskExtensionImpl as e   where  e.active = 1      ");
      Collection<EngineerProcessTaskExtension> _list = query.list();
      final Function1<EngineerProcessTaskExtension,Boolean> _function = new Function1<EngineerProcessTaskExtension,Boolean>() {
          public Boolean apply(final EngineerProcessTaskExtension it) {
            BusinessProcess _businessProcess = it.getBusinessProcess();
            return Boolean.valueOf((_businessProcess instanceof CostEstimationProcesss));
          }
        };
      Iterable<EngineerProcessTaskExtension> _filter = IterableExtensions.<EngineerProcessTaskExtension>filter(_list, _function);
      final Function1<EngineerProcessTaskExtension,Boolean> _function_1 = new Function1<EngineerProcessTaskExtension,Boolean>() {
          public Boolean apply(final EngineerProcessTaskExtension it) {
            BusinessProcess _businessProcess = it.getBusinessProcess();
            BusinessProcess _businessProcess_1 = costEstimation.getBusinessProcess();
            boolean _equals = Objects.equal(_businessProcess, _businessProcess_1);
            return Boolean.valueOf(_equals);
          }
        };
      Iterable<EngineerProcessTaskExtension> _filter_1 = IterableExtensions.<EngineerProcessTaskExtension>filter(_filter, _function_1);
      final Procedure1<EngineerProcessTaskExtension> _function_2 = new Procedure1<EngineerProcessTaskExtension>() {
          public void apply(final EngineerProcessTaskExtension it) {
            CostEstimationDAOImpl.this.taskExtensionDelete.deleteTaskExt(it);
          }
        };
      IterableExtensions.<EngineerProcessTaskExtension>forEach(_filter_1, _function_2);
      BusinessProcess _businessProcess_1 = costEstimation.getBusinessProcess();
      this.businessProcessDelete.deleteBusinessProcess(_businessProcess_1);
    }
  }
}
